package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppRoomType;
import com.vf.admin.miaodong.service.AfterRoomTypeService;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房间分类管理
 */
@Controller
@RequestMapping("/after/roomcat")
public class AfterRoomCatController extends BaseController {

    @Autowired
    AfterRoomTypeService afterRoomTypeService;

    /**
     * 获取所有房间分类
     */
    @RequestMapping("/list")
    public String getAllUser(SplitPageBean splitPage,String name) {
        Map<String,Object> mep=new HashMap<>(16);
        mep.put("name",name);
        List<AppRoomType> maps = afterRoomTypeService.list(mep,splitPage);
        request.setAttribute("list",maps);
        request.setAttribute("name",name);
        request.setAttribute("splitPage",splitPage);
        return this.sucess("/web/after/roomcat/list");
    }

    /**
     * 房间类型详情
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id){
        try{
            AppRoomType appSysRoomType = afterRoomTypeService.get(id);
            request.setAttribute("roomtype",appSysRoomType);
            return this.sucess("/web/after/roomcat/edit");
        }catch (Exception e){
            return this.error("系统繁忙");
        }
    }

    /**
     * 编辑房间类型信息
     * @param appSysRoomType
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppRoomType appSysRoomType){
        try{
            if(StringEx.isNull(appSysRoomType.getName())){
                return this.errorJSON("房间类型名称不能为空");
            }
            if(StringEx.isNull(appSysRoomType.getId())){
                afterRoomTypeService.save(appSysRoomType);
            }else{
                AppRoomType appSysRoomType1 = afterRoomTypeService.get(appSysRoomType.getId());
                appSysRoomType1.setName(appSysRoomType.getName());
                appSysRoomType1.setXingzhi(appSysRoomType.getXingzhi());
                if(StringEx.isNull(appSysRoomType.getState())){
                    appSysRoomType1.setState(2);
                }else{
                    appSysRoomType1.setState(appSysRoomType.getState());
                }
                appSysRoomType1.setPaixu(appSysRoomType.getPaixu());
                afterRoomTypeService.save(appSysRoomType1);
            }
            return this.sucessJSON("保存成功");
        }catch (Exception e){
            return this.errorJSON("系统繁忙");
        }
    }

}
